package com.thomas.jaspersoft.studio.example.service;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thomas.jaspersoft.studio.example.json.Customer;
import com.thomas.jaspersoft.studio.example.json.CustomerRepostiory;
import com.thomas.jaspersoft.studio.example.json.Customers;
import com.thomas.jaspersoft.studio.example.request.JasperParamter;
import com.thomas.jaspersoft.studio.example.request.ReportRequest;
import com.thomas.jaspersoft.studio.example.request.ResourceType;

import net.sf.jasperreports.engine.JRException;

@Service
public class JasperExampleService {
	
	private static final Log LOG = LogFactory.getLog(JasperExampleService.class);
	
	@Autowired
	private JasperParamter jasperParamter;
	
	@Autowired
	private JasperService jasperService;
	
	@Autowired
	private CustomerRepostiory customerRepostiory;
	
	public String generateReport(ReportRequest reportRequest) throws JRException, SQLException, IOException{			
		if(ResourceType.json.name().equalsIgnoreCase(reportRequest.getResourceType())){
			if(StringUtils.isEmpty(reportRequest.getStrJson())){
				reportRequest.setStrJson(prepareDefaultJson());
			}
		}
		Map<String, Object> parameters = this.prepareParameters(reportRequest);
		return this.jasperService.generateReport(reportRequest, parameters);
	}
	
	private String prepareDefaultJson() throws JsonGenerationException, JsonMappingException, IOException {
		List<Customer> customerList = customerRepostiory.findAll();   
		Customers customers = new Customers();
		customers.setCustomers(customerList);
		ObjectMapper mapper = new ObjectMapper();
		//Object to JSON in file
		mapper.writeValue(new File(this.jasperParamter.getJasperTempFolder() + "/" + "customer.json"), customers);
		//Object to JSON in String
		String jsonInString = mapper.writeValueAsString(customers);
		LOG.debug(jsonInString);
		return jsonInString;
	}

	private Map<String, Object>  prepareParameters(ReportRequest reportRequest) throws UnsupportedEncodingException{
		String reportName = reportRequest.getReportName();
		String parentFolderName = reportName;
		Map<String, Object> parameters = new HashMap<String, Object>();
		switch(reportName){
			case "Demo1" : 
				parameters.put("year", 2018);
				parameters.put("month", 1);
				parameters.put("sub_dir", this.jasperParamter.getJasperTempFolder() + "/" + parentFolderName);
				break;
			case "excel_example" : break;
			case "Json_example" :
				InputStream inputStream = new ByteArrayInputStream(reportRequest.getStrJson().getBytes("UTF-8"));
	            parameters.put("JSON_INPUT_STREAM", inputStream);
				break;
			default : break;
		}
		return parameters;
	}
}
